/*
 *  MWSolve.cpp
 *  T3nsors2
 *
 *  Created by Michael Barriault on 10-07-08.
 *  Copyright 2010 University of Guelph. All rights reserved.
 *
 */


#include "MW.h"
#include <cmath>
#include <iostream>
#include <sys/time.h>
using namespace t3;
using std::cout;
using std::endl;
using std::flush;

void MW::Solve(double tn, double s) {
	cout << O->d(0) << "\n";
	double dt = s*O->d(0);
	int N = int(tn/dt);
	cout << "Initializing values" << endl;
	/*	FOR(o,O->size()) E->c(0)->at(o) = cos(10*M_PI*x->c(2)->at(o));
	 FOR(o,O->size()) E->c(1)->at(o) = sin(10*M_PI*x->c(2)->at(o));
	 FOR(o,O->size()) B->c(0)->at(o) = -sin(10*M_PI*x->c(2)->at(o));
	 FOR(o,O->size()) B->c(1)->at(o) = cos(10*M_PI*x->c(2)->at(o));*/
	int z0 = (O->at(2)-1)/4;
	double R;
	FOR(i,O->at(0)) FOR(j,O->at(1)) for ( int k=z0; k<3*z0; k++ ) {
		R = pow(pow(x->c(2)->pt(i,j,k),2)-1,4);
		E->c(0)->pt(i,j,k) = R*cos(9*M_PI*x->c(2)->pt(i,j,k));
		E->c(1)->pt(i,j,k) = R*sin(9*M_PI*x->c(2)->pt(i,j,k));
		B->c(0)->pt(i,j,k) = -R*sin(9*M_PI*x->c(2)->pt(i,j,k));
		B->c(1)->pt(i,j,k) = R*cos(9*M_PI*x->c(2)->pt(i,j,k));
	}
	double t = 0;
	cout << "Evolution loop" << endl;
	struct timeval start,first;
	struct timezone tzp;
	gettimeofday(&start,&tzp);
	FOR(n,N) {
		gettimeofday(&first,&tzp);
		Print(t, Phi);
		Print(t, Psi);
		Print(t, E);
		Print(t, B);
		t += dt;
		RK(dt);
		cout << "Time remaining: " << timer(first, N-n) << "       " << t << "                 \r" << flush;
	}
	cout << "Total elapsed time: " << timer(start) << "                                   " << endl;
}

